#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using PLACEHR.Web.Utilities;

#endregion

namespace PLACEHR.Web.Categories.Management.Controls
{
    public partial class EmployeeVacationControl : UserControl
    {
        public EmployeeVacationControl()
        {
            InitializeComponent();
            InitialData();

            Retrieve();
        }

        private void InitialData()
        {
            lblEmployeeName.Text = LanLayer.GetTrans(122) + ":";
            lblDepartment.Text = LanLayer.GetTrans(125) + ":";
            lblOffice.Text = LanLayer.GetTrans(124) + ":";
            lblVacationType.Text = LanLayer.GetTrans(123) + ":";

            colClosingBalance.Text = LanLayer.GetTrans(130);
            colDepartment.Text = LanLayer.GetTrans(125);
            colLength.Text = LanLayer.GetTrans(127);
            colName.Text = LanLayer.GetTrans(122);
            colOffice.Text = LanLayer.GetTrans(124);
            colOpenningBalance.Text = LanLayer.GetTrans(129);
            colType.Text = LanLayer.GetTrans(123);

            cbbVacationType.Items.Clear();
            List<VacationType_lkp> lstVacationType = Global.Context.VacationType_lkp.ToList();

            ComboBoxItem item1 = new ComboBoxItem();
            item1.Text = "ANN";
            item1.Value = "ANN";

            cbbVacationType.Items.Add(item1);

            ComboBoxItem item2 = new ComboBoxItem();
            item2.Text = "SKL";
            item2.Value = "SKL";

            cbbVacationType.Items.Add(item2);
            cbbVacationType.SelectedIndex = 0;

            cbbOffice.Items.Clear();
            List<Office> lstOffice = Global.Context.Offices.ToList();
            cbbOffice.Items.Add("");
            foreach (var office in lstOffice)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Text =  LanLayer.GetTrans(office.ContentId.Value);
                item.Value = office;

                cbbOffice.Items.Add(item);
            }

            cbbDepartment.Items.Clear();
            List<Department> lstDepartment = Global.Context.Departments.ToList();
            cbbDepartment.Items.Add("");
            foreach (var department in lstDepartment)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Text = LanLayer.GetTrans(department.ContentId.Value);
                item.Value = department;

                cbbDepartment.Items.Add(item);
            }
        }

        private void Retrieve()
        {
            var vacationList = Global.Context.VacationBases.Where(o => o.IsCurrent && o.IsDeleted == false && o.Employee.RoleID == 1);
            if (!string.IsNullOrWhiteSpace(tbEmployeeName.Text))
            {
                vacationList = vacationList.Where(o => o.Employee.EnglishName.Contains(tbEmployeeName.Text) || o.Employee.ChineseName.Contains(tbEmployeeName.Text));
            }

            if (!string.IsNullOrWhiteSpace(cbbVacationType.Text))
            {
                ComboBoxItem item = cbbVacationType.SelectedItem as ComboBoxItem;
                string typeCode = item.Value as string;
                vacationList = vacationList.Where(o => o.VacationType_lkp.VacationTypeCode == typeCode);
            }

            if (!string.IsNullOrWhiteSpace(cbbOffice.Text))
            {
                ComboBoxItem item = cbbOffice.SelectedItem as ComboBoxItem;
                Office office = item.Value as Office;
                vacationList = vacationList.Where(o => o.Employee.OfficeDepartment_lnk.First().OfficeCode == office.OfficeCode);
            }

            if (!string.IsNullOrWhiteSpace(cbbDepartment.Text))
            {
                ComboBoxItem item = cbbDepartment.SelectedItem as ComboBoxItem;
                Department department = item.Value as Department;
                vacationList = vacationList.Where(o => o.Employee.OfficeDepartment_lnk.First().DepartmentCode == department.DepartmentCode);
            }

            lvwData.Items.Clear();
            lvwData.BeginUpdate();
            int i = 1;
            foreach (var vacation in vacationList.ToList())
            {
                ListViewItem lvwItem = new ListViewItem("");
                lvwItem.SubItems.AddRange(new string[8]);

                ComboBoxItem item = cbbVacationType.SelectedItem as ComboBoxItem;
                string typeCode = item.Value as string;
                var officeDepartment_lnk = vacation.Employee.OfficeDepartment_lnk.FirstOrDefault();

                var length = vacation.Employee.EmployeeVacations.Where(o=>o.Status == "Approved" && o.OperateTime.HasValue && o.OperateTime.Value > DateTime.Today.AddDays(-DateTime.Today.Day + 1) && o.VacationTypeCode == typeCode).Sum(o=>o.Length);

                lvwItem.SubItems[0].Text = i.ToString();
                lvwItem.SubItems[1].Text = vacation.Employee.EnglishName + "(" + vacation.Employee.ChineseName + ")";
                lvwItem.SubItems[2].Text = LanLayer.GetTrans(vacation.VacationType_lkp.Content_id.Value);
                lvwItem.SubItems[3].Text = officeDepartment_lnk == null ? "" : LanLayer.GetTrans(officeDepartment_lnk.Office.ContentId.Value);
                lvwItem.SubItems[4].Text = officeDepartment_lnk == null ? "" : LanLayer.GetTrans(officeDepartment_lnk.Department.ContentId.Value);
                lvwItem.SubItems[5].Text = vacation.InitialHours.HasValue ? vacation.InitialHours.Value.ToString("0.00") : "";
                lvwItem.SubItems[6].Text = length.HasValue? length.Value.ToString("0.00") : "";
                lvwItem.SubItems[7].Text = vacation.FinalHours.HasValue ? vacation.FinalHours.Value.ToString("0.00") : "";

                lvwItem.Tag = vacation;

                lvwData.Items.Add(lvwItem);
                i++;
            }
        }

        private void queryControl1_FilterQueryClick(object sender, EventArgs e)
        {
            Retrieve();
        }
    }
}
